﻿using Reborn.Net.Http.Extension;
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Serializers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using RestSharp.Serialization;

namespace Reborn.Net.Http.Drive
{
    public class RestSharpDrive : Drive
    {
        RestClient _restClient = null;
        public RestSharpDrive(HttpConfig httpConfig) : base(httpConfig)
        {
            _restClient = new RestClient();
            Timeout = 180 * 1000;
        }


        Response ConvertResponse(IRestResponse restResponse)
        {
            Response response = new();

            switch (restResponse.ResponseStatus)
            {
                case ResponseStatus.None:
                    response.StatusCode = System.Net.HttpStatusCode.Created;
                    break;
                case ResponseStatus.Completed:
                    response.StatusCode = System.Net.HttpStatusCode.OK;
                    break;
                case ResponseStatus.Error:
                    response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                    break;
                case ResponseStatus.Aborted:
                    response.StatusCode = System.Net.HttpStatusCode.BadRequest;
                    break;
                case ResponseStatus.TimedOut:
                    response.StatusCode = System.Net.HttpStatusCode.RequestTimeout;
                    break;

            }
            response.Content = restResponse.Content;


            return response;
        }

        /// <summary>
        /// 请求数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public override Response Get(string url, Request request = null)
        {
            var restRequest = new RestRequest(url);
            restRequest.Timeout = Timeout;
            var response = _restClient.Get(restRequest);

            return ConvertResponse(response);
        }



        public override async Task<Response> GetAsync(string url, Request request = null)
        {
            var restRequest = new RestRequest(url);
            restRequest.Timeout = Timeout;

            var response = await _restClient.TryGetAsync(restRequest);

            return response;
        }

        public override Response Post(string url,string token, Request request = null)
        {
            var restRequest = new RestRequest(url);
            restRequest.Timeout = Timeout;
            restRequest.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
            restRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            restRequest.AddHeader("Accept", "*/*");
            restRequest.AddBody(token);
            var response =  _restClient.Post(restRequest);

            return ConvertResponse(response);
        }

        public override async Task<Response> PostAsync(string url,string token, Request request = null)
        {
            var restRequest = new RestRequest(url);
            restRequest.Timeout = Timeout;

            restRequest.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
            restRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            restRequest.AddHeader("Accept", "*/*");
            restRequest.AddBody(token);

            var response = await _restClient.TryPostAsync(restRequest);

            return response;
        }

        public override Response PostJson(string url, JToken postArgs, Request request = null)
        {
            string json = JsonToString(postArgs);

            var restRequest = new RestRequest(url);
            restRequest.Timeout = Timeout;

            restRequest.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
            restRequest.AddHeader("Content-Type", "application/json");
            restRequest.AddHeader("Accept", "*/*");
            restRequest.AddBody(json, ContentType.Json);

            var response = _restClient.TryPost(restRequest);

            return response;
        }

        public override async Task<Response> PostJsonAsync(string url, JToken postArgs, Request request = null)
        {
            string json = JsonToString(postArgs);

            var restRequest = new RestRequest(url);
            restRequest.Timeout = Timeout;

            restRequest.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
            restRequest.AddHeader("Content-Type", "application/json");
            restRequest.AddHeader("Accept", "*/*");
            restRequest.AddBody(json, ContentType.Json);

            var response = await _restClient.TryPostAsync(restRequest);

            return response;
        }

        public override Response Post(string url, Dictionary<string, string> dicArgs, Request request = null)
        {
            string postStr = DictionaryToString(dicArgs);

            return Post(url, postStr, request);
        }

        public override Task<Response> PostAsync(string url, Dictionary<string, string> dicArgs, Request request = null)
        {
            string postStr = DictionaryToString(dicArgs);

            return PostAsync(url, postStr, request);
        }

        public override byte[] GetByte(string url)
        {
            var restRequest = new RestRequest(url);
            return _restClient.DownloadData(restRequest);
        }

        public override Task<byte[]> GetByteAsync(string url)
        {
            var restRequest = new RestRequest(url);
            // return _restClient.as(restRequest);

            return null;
        }
    }
}
